package com.allen.study.infrastructure.persistence;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.allen.study.application.api.request.ApprovalRecordQueryRequest;
import com.allen.study.application.api.response.ApprovalRecordQueryResponse;
import com.allen.study.application.repository.IApprovalRecordReadModelRepo;
import com.allen.study.domain.entity.ApprovalRecord;
import com.allen.study.domain.repository.IApprovalRecordRepo;
import com.allen.study.common.base.Pagination;
import com.allen.study.infrastructure.persistence.assembler.ApprovalRecordPOAssembler;
import com.allen.study.infrastructure.persistence.dao.IApprovalRecordMapper;
import com.allen.study.infrastructure.persistence.po.ApprovalRecordPO;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 审批记录表
 *
 * @author AllenSun
 * @since 2025-04-23 20:46
 */
@Service
@AllArgsConstructor
public class ApprovalRecordRepoImpl extends ServiceImpl<IApprovalRecordMapper, ApprovalRecordPO>
        implements IApprovalRecordRepo, IApprovalRecordReadModelRepo {

    /**
     * 审批记录表持久化对象结构映射
     */
    private final ApprovalRecordPOAssembler approvalRecordPOAssembler;

    @Override
    public List<ApprovalRecordQueryResponse> query(Pagination pagination, ApprovalRecordQueryRequest queryRequest) {
        // 构造查询条件
        LambdaQueryWrapper<ApprovalRecordPO> lambdaQueryWrapper = Wrappers.lambdaQuery();


        // 如果  不为空
        if (ObjectUtils.isNotEmpty(queryRequest.getProcessInstanceId())) {
            lambdaQueryWrapper.eq(ApprovalRecordPO::getProcessInstanceId, queryRequest.getProcessInstanceId());
        }
        // 如果  不为空
        if (ObjectUtils.isNotEmpty(queryRequest.getApproverId())) {
            lambdaQueryWrapper.eq(ApprovalRecordPO::getApproverId, queryRequest.getApproverId());
        }
        // 如果  不为空
        if (ObjectUtils.isNotEmpty(queryRequest.getApprovalResult())) {
            lambdaQueryWrapper.eq(ApprovalRecordPO::getApprovalResult, queryRequest.getApprovalResult());
        }
        // 如果  不为空
        if (ObjectUtils.isNotEmpty(queryRequest.getApprovalReason())) {
            lambdaQueryWrapper.eq(ApprovalRecordPO::getApprovalReason, queryRequest.getApprovalReason());
        }
        // 如果  不为空
        if (ObjectUtils.isNotEmpty(queryRequest.getApprovalTime())) {
            lambdaQueryWrapper.eq(ApprovalRecordPO::getApprovalTime, queryRequest.getApprovalTime());
        }

        // 默认按照主键倒序排序
        lambdaQueryWrapper.orderByDesc(ApprovalRecordPO::getId);

        // 分页查询
        Page<ApprovalRecordPO> page = super.page(Page.of(pagination.getPageNumber(), pagination.getPageSize()), lambdaQueryWrapper);
        pagination.setTotal(page.getTotal());
        // 审批记录表持久化对象 转 审批记录表查询响应对象
        return approvalRecordPOAssembler.toQueryResponse(page.getRecords());
    }

    @Override
    public ApprovalRecord queryById(Long approvalRecordId) {
        // 根据主键查询审批记录表
        ApprovalRecordPO approvalRecordPO = super.getById(approvalRecordId);
        // 审批记录表持久化对象 转 审批记录表响应对象
        return approvalRecordPOAssembler.toEntity(approvalRecordPO);
    }

    @Override
    public void create(ApprovalRecord approvalRecord) {
        // 审批记录表实体转审批记录表持久化对象
        ApprovalRecordPO approvalRecordPO = approvalRecordPOAssembler.assembler(approvalRecord);

        // 创建审批记录表
        super.save(approvalRecordPO);
    }

    @Override
    public void deleteById(Long approvalRecordId) {
        // 物理删除审批记录表
        super.removeById(approvalRecordId);
    }

    @Override
    public void updateById(ApprovalRecord approvalRecord) {
        // 审批记录表实体转审批记录表持久化对象
        ApprovalRecordPO approvalRecordPO = approvalRecordPOAssembler.assembler(approvalRecord);
        // 更新审批记录表
        super.updateById(approvalRecordPO);
    }
}
